//
//  GSRootViewController.m
//  GameSky
//
//  Created by lanou on 16/1/9.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//

#import "GSRootViewController.h"
#import "GSRootTabBarController.h"
#import "GSRootTableViewCell.h"
#import "GSAppUIContentViewController.h"
#import "GSFeedBackViewController.h"
#import "GSMoreSetViewController.h"
#import "GSImportantNewsViewController.h"
#import <SDImageCache.h>
#import "FKViewController.h"
#import "GSLoginViewController.h"
#import "GSSearchViewController.h"
typedef NS_ENUM(NSInteger, LeftMenu){
    Search,
    News,
    Collect,
    ClearCache,
    Night,
    PicMode,
    FeedBack,
    MoreSet,
} ;

@interface GSRootViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) GSRootTabBarController *rootTBC;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataArray;// 标题数组
@property (nonatomic, strong) NSArray *picArray;// 图片数组

@property (nonatomic, strong) GSFeedBackViewController *feedBack;

@property (nonatomic, strong) GSMoreSetViewController *moreSet;
// 要闻
@property (nonatomic, strong) GSImportantNewsViewController *importtantNewsVC;

@property (nonatomic, strong) UINavigationController *navC;

// 登陆按钮
@property (nonatomic, strong) UIButton *iconBtn;
// 判断是否 登陆
@property (nonatomic, assign) BOOL isLogin;

// 搜索 界面
@property (nonatomic, strong) UINavigationController *searchNavC;

@property (nonatomic, strong) FKViewController *fkLink;
@end

@implementation GSRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 获取  本地 存储的数据
    self.isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"isLogin"];
    [self addSubviews];
    
}

- (void)addSubviews
{
    // 1.添加TableView;
    self.dataArray = @[@"搜索",@"要闻",@"连连看",@"清空缓存",@"夜间模式",@"3G/4G无图",@"我要反馈",@"更多设置"];
    self.picArray = @[@"drawer_search",@"drawer_news",@"drawer_collect",@"drawer_clear_cache",@"drawer_night",@"drawer_pic_mode",@"drawer_feedback",@"drawer_more_set"];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 220, kScreenWidth * 0.7, kScreenHeight - 150) style:(UITableViewStylePlain)];
    // 夜间模式 
    self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = (kScreenHeight - 220) / self.dataArray.count;
     [self.tableView registerClass:[GSRootTableViewCell class] forCellReuseIdentifier:@"GSRootTableViewCell"];
    [self.view addSubview:self.tableView];
    
    // 2.添加表头
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth * 0.7, 200)];

    _iconBtn = [[UIButton alloc] initWithFrame:CGRectMake(headerView.width / 3, 50, headerView.width / 3, 120)];
    _iconBtn.imageEdgeInsets = UIEdgeInsetsMake(0, headerView.width / 12, 120 - headerView.width / 3, headerView.width / 12);
    _iconBtn.titleEdgeInsets = UIEdgeInsetsMake(headerView.width / 6, -headerView.width / 6, 0, 0);
    [_iconBtn setImage:[UIImage imageNamed:@"common_User_DefaultHeadImage_UIMode_Day"] forState:(UIControlStateNormal)];
    _iconBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
// 判断是否 登陆 ,更改 标题
    if (_isLogin) {
        NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
        [_iconBtn setTitle:name forState:(UIControlStateNormal)];
    }else{
    [_iconBtn setTitle:@"点击登录" forState:(UIControlStateNormal)];
    }
    [headerView addSubview:_iconBtn];

// 添加方法
    [_iconBtn addTarget:self action:@selector(iconBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    headerView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:headerView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    GSRootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GSRootTableViewCell" forIndexPath:indexPath];

    cell.title.text = self.dataArray[indexPath.row];
    cell.leftImage.image = [UIImage imageNamed:self.picArray[indexPath.row]];
    
    NSString *switchStatus  = [[NSUserDefaults standardUserDefaults]objectForKey:@"switch"];
    NSString *picMode = [[NSUserDefaults standardUserDefaults]objectForKey:@"picMode"];
    
    if (indexPath.row == 4) {
        if ([[kUserDefaults valueForKey:@"nightMode"] isEqualToString:@"on"]) {
            [cell.rightBtn setImage:[UIImage imageNamed:switchStatus] forState:(UIControlStateNormal)];
        } else {
            [cell.rightBtn setImage:[UIImage imageNamed:@"off"] forState:(UIControlStateNormal)];
        }
    }
    
    if (indexPath.row == 5) {
        if ([[kUserDefaults valueForKey:@"picMode"] isEqualToString:@"on"]) {
            [cell.rightBtn setImage:[UIImage imageNamed:picMode] forState:(UIControlStateNormal)];
        } else {
            [cell.rightBtn setImage:[UIImage imageNamed:@"off"] forState:(UIControlStateNormal)];
        }
    }

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 立即取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSUserDefaults *deafult = [NSUserDefaults standardUserDefaults];
    switch (indexPath.row) {
        case Search:{
            GSSearchViewController *searchVC = [[GSSearchViewController alloc] init];
            searchVC.number = 1000;
            self.searchNavC = [[UINavigationController alloc] initWithRootViewController:searchVC];
            
            _searchNavC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
            
            // 搜索界面 已经 写好了... 不想改,  所以 按钮就在这加了...
            UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"common_Icon_Back_20x20_UIMode_Day@2x"] style:(UIBarButtonItemStylePlain) target:self action:@selector(backBarButtonItem:)];
            searchVC.navigationItem.leftBarButtonItem = item;
            searchVC.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
            
            [[UIApplication sharedApplication].keyWindow addSubview:_searchNavC.view];
            [UIView animateWithDuration:0.5 animations:^{
                
                _searchNavC.view.transform = CGAffineTransformTranslate(_searchNavC.view.transform, -kScreenWidth, 0);
            }];
        }

            break;
        case News:{
            self.importtantNewsVC = [[GSImportantNewsViewController alloc] init];
            self.importtantNewsVC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
            [[UIApplication sharedApplication].keyWindow addSubview:self.importtantNewsVC.view];
            [UIView animateWithDuration:0.5 animations:^{
                self.importtantNewsVC.view.transform = CGAffineTransformTranslate(self.importtantNewsVC.view.transform, -kScreenWidth, 0);
            }];
        }
            break;
        case Collect:
        {
            self.fkLink = [[FKViewController alloc] init];
            
            
            self.fkLink.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
            
            
            [[UIApplication sharedApplication].keyWindow addSubview:self.fkLink.view];
            [UIView animateWithDuration:0.5 animations:^{
                
                self.fkLink.view.transform = CGAffineTransformTranslate(self.fkLink.view.transform, -kScreenWidth, 0);
            }];
            
            
        }

            break;
            
        case ClearCache:{
            
            // 单例(一个应用程序只有一个对象)
            
            SDImageCache *sdImageCache = [SDImageCache sharedImageCache];
            
            NSString *str = [NSString stringWithFormat:@"缓存大小%.2fK.是否清除缓存?", [sdImageCache getSize]/1024.0];
            
            NSString *cancelStr = @"取消";
            
            if ([sdImageCache getSize] == 0) {
                
                str = @"您还没有缓存, 不需要清理哦!";
                
                cancelStr = nil;
            }
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:cancelStr otherButtonTitles:@"确定", nil];
            
            // 这里执行协议方法的对象是这个视图控制器对象(协议方法是给别人用的)
            
            alertView.delegate = self;
            
            [alertView show];

        }
            break;
        case Night:{
            GSRootTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            if ([cell.rightBtn.currentImage isEqual:[UIImage imageNamed:@"off"]] ) {
                //  打开夜间模式
                [kNotificationCenter postNotificationName:DKNightVersionNightFallingNotification object:self];
                [cell.rightBtn setImage:[UIImage imageNamed:@"on"] forState:(UIControlStateNormal)];
                [deafult setObject:@"on" forKey:@"nightMode"];
            } else {
                // 关闭夜间模式
                [kNotificationCenter postNotificationName:DKNightVersionDawnComingNotification object:self];
                [cell.rightBtn setImage:[UIImage imageNamed:@"off"] forState:(UIControlStateNormal)];
                [deafult setObject:@"off" forKey:@"nightMode"];
            }
        }
            break;
            
        case PicMode:{
            
            GSRootTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            
            if ([cell.rightBtn.currentImage isEqual:[UIImage imageNamed:@"off"]] ) {
                // 打开移动网络无图模式
                [cell.rightBtn setImage:[UIImage imageNamed:@"on"] forState:(UIControlStateNormal)];
                [deafult setObject:@"on" forKey:@"picMode"];
            } else {
                // 关闭移动网络无图模式
                [cell.rightBtn setImage:[UIImage imageNamed:@"off"] forState:(UIControlStateNormal)];
                [deafult setObject:@"off" forKey:@"picMode"];
            }
        }
            break;
            
        case FeedBack:{
            
            self.feedBack = [[GSFeedBackViewController alloc]init];
            
            self.feedBack.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
            
            
            [[UIApplication sharedApplication].keyWindow addSubview:self.feedBack.view];
            
            
            [UIView animateWithDuration:0.5 animations:^{
               
                self.feedBack.view.transform = CGAffineTransformTranslate(self.feedBack.view.transform, -kScreenWidth, 0);
                
            }];
        }
            break;
            
        case MoreSet:
        {
            self.moreSet = [[GSMoreSetViewController alloc]init];
            
            self.moreSet.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
            
            [[UIApplication sharedApplication].keyWindow addSubview:self.moreSet.view];
            
            
            [UIView animateWithDuration:0.5 animations:^{
                
                self.moreSet.view.transform = CGAffineTransformTranslate(self.moreSet.view.transform, -kScreenWidth, 0);
                
            }];
        }
            break;
            
        default:
            break;
    }

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

{
    
    if (buttonIndex == 1) {
        
        [[SDImageCache sharedImageCache] clearDisk];
        
    }
    
}


// 改变状态栏的颜色
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

#pragma mark------------登陆按钮-------------
- (void)iconBtn:(UIButton *)button{
    if (_isLogin) {
        // 弹窗
       UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"是否退出登陆" preferredStyle:(UIAlertControllerStyleAlert)];
        //
        UIAlertAction *makeSure = [UIAlertAction actionWithTitle:@"退出" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isLogin"];

             [_iconBtn setTitle:@"点击登录" forState:(UIControlStateNormal)];
            GSLoginViewController *loginVC = self.navC.viewControllers[0];
        loginVC.loginView.passwordTextField.text = @"";
            _isLogin = NO;
            [alertController dismissViewControllerAnimated:YES completion:nil];
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertController addAction:makeSure];
        [alertController addAction:cancel];
        [self presentViewController:alertController animated:YES completion:nil];
    }else{

    if (self.navC == nil) {
        // 初始化登陆界面
        GSLoginViewController *loginVC = [[GSLoginViewController alloc] init];
        __weak GSRootViewController *mySelf = self;
// 登陆成功后,  block 传值
        loginVC.isLoginBlock = ^void(BOOL isLogin,NSString *name){
            mySelf.isLogin = isLogin;
            [[NSUserDefaults standardUserDefaults] setBool:isLogin forKey:@"isLogin"];
            [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"userName"];
            
            [mySelf.iconBtn setTitle:name forState:(UIControlStateNormal)];
        };
    // 添加导航控制器
        self.navC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        self.navC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
        [[UIApplication sharedApplication].keyWindow addSubview:_navC.view];
    }
 
    [UIView animateWithDuration:0.5 animations:^{
        self.navC.view.transform = CGAffineTransformTranslate(self.navC.view.transform, -kScreenWidth, 0);
        
    }];
  }
}
- (void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerDidOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}

- (void)drawerControllerWillClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerDidClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}
#pragma mark--------------搜索界面的返回按钮实现--------
- (void)backBarButtonItem:(UIBarButtonItem *)item{
    [UIView animateWithDuration:0.5 animations:^{
        _searchNavC.view.transform = CGAffineTransformTranslate(_searchNavC.view.transform, kScreenWidth, 0);
    }];
}

@end
